<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<script>
    var socket;
    // 判断当前浏览器是否支持websocket
    if (window.WebSocket){
        socket = new WebSocket("ws://localhost:7000/hello");
        // 收到服务端回传的消息
        socket.onmessage = function (ev) {
            var tr = document.getElementById('responseText');
            tr.value = tr.value + "\n" + ev.data;
        }

        // 感知连接开启
        socket.onopen = function (ev) {
            document.getElementById('responseText').value = '连接开启~';
        }

        // 感知连接关闭
        socket.onclose = function (ev){
            var tr = document.getElementById('responseText')
            tr.value = tr.value + "\n" + "连接关闭~";
        }
    }else{
        alert("当前浏览器不支持websocket~");
    }

    function send(message){
        if(!window.socket){
            return;
        }
        if(socket.readyState == WebSocket.OPEN){
            document.getElementById('message').value = '';
            // 发送消息倒服务器
            socket.send(message);
        }else{
            alert("连接未开启~");
        }
    }
</script>
    <form onsubmit="return false">
        <textarea name="message" id="message" style="height: 300px; width: 300px"></textarea>
        <input type="button" value="发送消息" onclick="send(this.form.message.value)">
        <textarea id="responseText" style="height: 300px; width: 300px"></textarea>
        <input type="button" value="清空内容" onclick="document.getElementById('responseText').value=''">
    </form>
</body>
</html>